package aom.ju.ss.utils;

import android.annotation.SuppressLint;
import android.text.TextUtils;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

import aom.ju.ss.BuildConfig;
import aom.ju.ss.view.cut.DaoJiShi;


public class TimeUtil {
    public static DaoJiShi sysj2(String time) {
        try {
            long date = Long.parseLong(time) * 1000 - System.currentTimeMillis();
            if (date < 0) {
                return new DaoJiShi(0, 0, 0, 0, 0);
            }
            int day = (int) (date / (1000 * 60 * 60 * 24));
            int hour = (int) (date / (1000 * 60 * 60) - day * 24);
            int min = (int) ((date / (60 * 1000)) - day * 24 * 60 - hour * 60);
            int ss = (int) (date / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);

            return new DaoJiShi(day, hour, min, ss, 0);
        } catch (Exception e) {
        }
        return new DaoJiShi(0, 0, 0, 0, 0);
    }

    public static Long dateToStamp(String s) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = simpleDateFormat.parse(s);
        return date.getTime();
    }


    @SuppressLint("SimpleDateFormat")
    public static String getHourAndMin(long time) {
        SimpleDateFormat format = new SimpleDateFormat("HH:mm");
        return format.format(new Date(time));
    }

    @SuppressLint("SimpleDateFormat")
    public static String getMinAndSecond(long time) {
        SimpleDateFormat format = new SimpleDateFormat("mm:ss");
        return format.format(new Date(time));
    }

    @SuppressLint("SimpleDateFormat")
    public static String getYearAndMonth(long time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        return format.format(new Date(time));
    }

    @SuppressLint("SimpleDateFormat")
    public static String getYearMonthAndDay(long time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(new Date(time));
    }

    @SuppressLint("SimpleDateFormat")
    public static String getMonthAndDay(long time) {
        SimpleDateFormat format = new SimpleDateFormat("MM-dd");
        return format.format(new Date(time));
    }

    @SuppressLint("SimpleDateFormat")
    public static String getDateFormat(long time) {
        String timeString = getYearMonthAndDay(time);
        return timeString.replaceAll("-", ".");
    }

    @SuppressLint("SimpleDateFormat")
    public static String getYearMonthAndDayWithHour(long time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return format.format(new Date(time));
    }


    @SuppressLint("SimpleDateFormat")
    public static String getYearMonthAndDayWithHourmmss(long time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(new Date(time));
    }


    public long timeToStamp(String timers) {
        Date d = new Date();
        long timeStemp = 0;
        try {
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
            d = sf.parse(timers);// 日期转换为时间戳
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        timeStemp = d.getTime();
        return timeStemp;
    }


    /**
     * 年月日 换行 时分秒
     *
     * @param time
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    public static String HMS(long time) {
        SimpleDateFormat hms = new SimpleDateFormat("HH:mm:ss");

        return hms.format(new Date(time));
    }


    public static boolean startTimUtll(Long start) {
        Date dt = new Date();//获取当前时间
        Long time = dt.getTime();
        final int liangtian = 172800000;
        if (time + liangtian > start) {
            return false;
        } else {
            return true;

        }

    }


    /**
     * 转时间戳
     *
     * @param S
     * @return
     */
    public static long sszszc(String S) {

        if (S == null) {
            return 0;
        }
        if (S.contains("T")) {
            S = S.replaceAll("T", " ");
        }
        SimpleDateFormat dff = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);//输入的被转化的时间格式
        try {
            Date date1 = dff.parse(S);

            return date1.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static String sszTYMDHMS(String S) {

        SimpleDateFormat dff = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.ENGLISH);//输入的被转化的时间格式
        SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//需要转化成的时间格式
        try {
            Date date1 = dff.parse(S);
            return df1.format(date1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "";
    }


    public static String sysj(long time) {
        try {
            long date = time - System.currentTimeMillis();
            if (date < 0) {
                return "";
            }
            long day = date / (1000 * 60 * 60 * 24);
            long hour = (date / (1000 * 60 * 60) - day * 24);
            long min = ((date / (60 * 1000)) - day * 24 * 60 - hour * 60);
            long ss = (date / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
            StringBuilder s = new StringBuilder();
            if (day != 0) {
                s.append(day).append("天");
            }
            if (hour != 0) {
                s.append(hour).append("时");
            }
            if (min != 0) {
                s.append(min).append("分");
            }
            if (ss != 0) {
                s.append(ss).append("秒");
            }
            return s.toString();
        } catch (Exception e) {
        }
        return "";
    }

    /**
     * 返回活动日期格式
     */
    public static String getDateFormat(Long start, Long end) {

        return TimeUtil.getYearMonthAndDay(start)
                + " ~ " + TimeUtil.getMonthAndDay(end);
    }

    /**
     * 返回活动时间格式
     * 如果时间一样  则只返回一个
     */
    public static String getTimeFormat(Long start, Long end) {

        String startTime = TimeUtil.getHourAndMin(start);

        String endTime = TimeUtil.getHourAndMin(end);

        if (startTime.equals(endTime)) {
            return startTime;
        }

        return startTime + " ~ " + endTime;
    }

    /**
     * @param millis 试试
     * @return "
     */
    public static String millisToStringShort(long millis) {
        StringBuilder strBuilder = new StringBuilder();
        long temp = millis;
        long hper = 60 * 60 * 1000;
        long mper = 60 * 1000;
        long sper = 1000;
        if (temp / hper > 0) {
            strBuilder.append(temp / hper).append(":");
        }
        temp = temp % hper;

        if (temp / mper > 0) {
            strBuilder.append(temp / mper).append(":");
        }
        temp = temp % mper;
        if (temp / sper > 0) {
            if (temp / sper < 10) {
                strBuilder.append("0").append(temp / sper);
            } else {
                strBuilder.append(temp / sper);
            }
        } else {
            strBuilder.append("00");
        }
        return strBuilder.toString();
    }

    public static final String FULL_STYLE = "yyyy-MM-dd HH:mm:ss";
    public static final String YMD_STYLE_1 = "yyyy-MM-dd";
    public static final String YMDHM_STYLE = "yyyy-MM-dd HH:mm";
    private static final String YMDHM = "yyyyMMddHHmm";
    private static final String YMDHMS = "yyyyMMddHHmmss";
    private static final String UTC = "yyyy-MM-dd'T'HH:mm:ss.SSS Z";

    public static int compareEndDays(String end) {
        try {
            long startTime = System.currentTimeMillis();
            long endTime = parseUTCText(end).getTime();
            if (endTime <= startTime) {
                return 0;
            } else {
                return (int) ((endTime - startTime));
            }
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    public static int compareEndDaysTwo(String end) {
        try {
            long startTime = System.currentTimeMillis();
            long endTime = parseUTCTextTwo(end).getTime();
            if (endTime <= startTime) {
                return 0;
            } else {
                return (int) ((endTime - startTime));
            }
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    public static long parseTimeFull(String time) {
        try {
            return new SimpleDateFormat(FULL_STYLE).parse(time).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
            return 0;
        }
    }

    public static long parseYmd(String time) {
        if (TextUtils.isEmpty(time)) return 0;
        try {
            return new SimpleDateFormat(YMD_STYLE_1).parse(time).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
            return 0;
        }
    }

    public static long parseYmdhm(String time) {
        try {
            return new SimpleDateFormat(YMDHM_STYLE).parse(time).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
            return 0;
        }
    }

    public static String millonToFull(long timestamp) {
        try {
            return new SimpleDateFormat(FULL_STYLE).format(timestamp);
        } catch (Exception e) {
            return "";
        }
    }

    public static String ymdhmToFull(String ymdhm) {
        if (TextUtils.isEmpty(ymdhm)) return ymdhm;
        return ymdhm + ":00";
    }

    public static String dateToYMD(Date date) {
        try {
            return new SimpleDateFormat(YMD_STYLE_1).format(date);
        } catch (Exception e) {
            return "";
        }
    }

    public static String dateToAny(Date date, String style) {
        try {
            return new SimpleDateFormat(style).format(date);
        } catch (Exception e) {
            return "";
        }
    }

    public static String millonToYMD(long date) {
        try {
            return new SimpleDateFormat(YMD_STYLE_1).format(date);
        } catch (Exception e) {
            return "";
        }
    }

    public static String fullStyle2Ymd(String source) {
        try {
            return new SimpleDateFormat(YMD_STYLE_1).format(new SimpleDateFormat(FULL_STYLE).parse(source));
        } catch (Exception e) {
            return "";
        }
    }

    public static String dateToYmdhm(Date date) {
        try {
            return new SimpleDateFormat(YMDHM_STYLE).format(date);
        } catch (Exception e) {
            return "";
        }
    }

    public static String dateToYmdNoSymbol(Date date) {
        try {
            return new SimpleDateFormat("yyyyMMdd").format(date);
        } catch (Exception e) {
            return "";
        }
    }

    public static String dateToFull(Date date) {
        try {
            return new SimpleDateFormat(FULL_STYLE).format(date);
        } catch (Exception e) {
            return "";
        }
    }

    public static String fileAuthTime() {
        return new SimpleDateFormat(YMDHM).format(new Date(System.currentTimeMillis()));
    }

    public static String getCurrentTime() {
        return new SimpleDateFormat(YMDHMS).format(new Date(System.currentTimeMillis()));
    }

    public static String utcToFull(String normal) {
        if (TextUtils.isEmpty(normal)) return "";
        try {
            normal = normal.replace("Z", " UTC");
            return dateToFull(new SimpleDateFormat(UTC).parse(normal));
        } catch (Exception e) {
            if (BuildConfig.DEBUG) e.printStackTrace();
            return normal;
        }
    }

    public static String utcToYmdhm(String normal) {
        if (TextUtils.isEmpty(normal)) return "";
        try {
            normal = normal.replace("Z", " UTC");
            return dateToYmdhm(new SimpleDateFormat(UTC).parse(normal));
        } catch (Exception e) {
            if (BuildConfig.DEBUG) e.printStackTrace();
            return normal;
        }
    }

    public static String utcToYmdNoSymbol(String normal) {
        if (TextUtils.isEmpty(normal)) return "";
        try {
            normal = normal.replace("Z", " UTC");
            return dateToYmdNoSymbol(new SimpleDateFormat(UTC).parse(normal));
        } catch (Exception e) {
            if (BuildConfig.DEBUG) e.printStackTrace();
            return normal;
        }
    }

    public static String utcToYmd(String normal) {
        if (TextUtils.isEmpty(normal)) return "";
        try {
            normal = normal.replace("Z", " UTC");
            return dateToYMD(new SimpleDateFormat(UTC).parse(normal));
        } catch (Exception e) {
            if (BuildConfig.DEBUG) e.printStackTrace();
            return normal;
        }

    }

    public static String utcToFullWithEver(String normal) {
        if (TextUtils.isEmpty(normal)) return "永久";
        try {
            normal = normal.replace("Z", " UTC");
            return dateToFull(new SimpleDateFormat(UTC).parse(normal));
        } catch (Exception e) {
            if (BuildConfig.DEBUG) e.printStackTrace();
            return normal;
        }

    }

    public static Date parseUTCTextTwo(String text) throws ParseException {
        if (TextUtils.isEmpty(text)) return new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        if (text.contains(".")) {
            String prefix = text.substring(0, text.indexOf("."));
            String suffix = text.substring(text.indexOf("."));
            if (suffix.length() >= 5) {
                suffix = suffix.substring(0, 4) + "Z";
            } else {
                int len = 5 - suffix.length();
                StringBuilder temp = new StringBuilder();
                temp.append(suffix.substring(0, suffix.length() - 1));
                for (int i = 0; i < len; i++) {
                    temp.append("0");
                }
                suffix = temp + "Z";
            }
            text = prefix + suffix;
        } else {
            text = text.substring(0, text.length() - 1) + ".000Z";
        }
        return sdf.parse(text);
    }

    public static Date parseUTCText(String text) throws ParseException {
        if (TextUtils.isEmpty(text)) return new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
        if (text.contains(".")) {
            String prefix = text.substring(0, text.indexOf("."));
            String suffix = text.substring(text.indexOf("."));
            if (suffix.length() >= 5) {
                suffix = suffix.substring(0, 4) + "Z";
            } else {
                int len = 5 - suffix.length();
                StringBuilder temp = new StringBuilder();
                temp.append(suffix.substring(0, suffix.length() - 1));
                for (int i = 0; i < len; i++) {
                    temp.append("0");
                }
                suffix = temp + "Z";
            }
            text = prefix + suffix;
        } else {
            text = text.substring(0, text.length() - 1) + ".000Z";
        }
        return sdf.parse(text);
    }

    public static String utcToNearText(String utcTime) {
        try {
            long timeNow = System.currentTimeMillis();
            long timeEnd = parseUTCText(utcTime).getTime();
            if (timeNow >= timeEnd) {
                return "已过期";
            } else {
                long timeDiff = timeEnd - timeNow;
                if (timeDiff < 3600 * 1000) {
                    return "即将结束";
                } else if (timeEnd - timeNow < 3600 * 1000 * 24) {
                    return "1天内结束";
                } else {
                    return millonToFull(timeEnd);
                }
            }
        } catch (ParseException e) {
            if (BuildConfig.DEBUG) e.printStackTrace();
            return utcToFull(utcTime);
        }
    }

    /**
     * 判断 start 是否大于 end
     *
     * @param star
     * @param end
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    public static boolean eqYMD(String star, String end) {

        LogUtils.e("star" + star + "end" + end);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        try {
            long startD = sdf.parse(star).getTime();

            long endD = sdf.parse(end).getTime();

            LogUtils.e(startD + "***" + endD + (startD > endD));
            return (startD >= endD);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }

    /**
     * 判断 start 是否大于 end
     *
     * @param star
     * @param end
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    public static boolean eqYMD2(String star, String end) {

        LogUtils.e("star" + star + "end" + end);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        try {
            long startD = sdf.parse(star).getTime();

            long endD = sdf.parse(end).getTime();

            LogUtils.e(startD + "***" + endD + (startD > endD));
            return (startD >= endD);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }

    public static String TimeDataInit(String field) {
        if (field == null) {
            return "";
        } else if (field.contains("T")) {
            return field.split("T")[0];

        }
        return field;
    }
}
